const db = require("../dbs/db");

class SecCategory_Controller {
    /**
     * 创建文章
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async create(ctx) {
        //接收客服端
        let req = ctx.request.body;
        if (req.name, req.parent) {
            try {
                const ret = await db.SecCategory.create({
                    name: req.name,
                    Superiorid: req.parent
                });
                const data = await db.SecCategory.findOne({
                    where: { name: ret.name }
                })

                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '创建成功',
                    data
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '创建失败',
                    data: err
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 200,
                msg: '参数不齐全'
            }
        }
    }

    /**
     * 获取文章详情
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async detail(ctx) {
        let id = ctx.params.id;
        if (id) {
            try {
                // 查询文章详情模型
                let data = await db.SecCategory.findOne({
                    where: { id: id }
                })
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '查询成功',
                    data
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '查询失败',
                    data
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 416,
                msg: '文章ID必须传'
            }
        }
    }

    static async update(ctx) {
        let req = ctx.request.body;
        let id = ctx.params.id;
        if (req.name && id) {
            try {
                await db.SecCategory.update({ name: req.name }, { where: { id: id } })
                let data = await db.SecCategory.findOne({
                    where: { name: req.name }
                })
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '更新成功',
                    data
                }
            } catch (err) {
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '更新失败',
                }
            }
        } else {
            ctx.response.status = 416;
            ctx.body = {
                code: 416,
                msg: '缺少ID值或name值'
            }
        }
    }

    static async delete(ctx) {
        let id = ctx.params.id;
        try {
            let data = await db.SecCategory.findOne({
                where: { id: id }
            })
            await db.SecCategory.destroy({
                where: {
                    id: id
                }
            })
            ctx.response.status = 200;
            ctx.body = {
                code: 200,
                msg: `删除${data.name}成功`,
            }
        } catch (err) {
            ctx.response.status = 412;
            ctx.body = {
                code: 412,
                msg: '删除失败',
            }
        }
    }

    static async list(ctx) {
        let data = await db.SecCategory.findAll(
            {  include: [{
                    model: db.TopCategory,
                    attributes: ['name']
                }],
            }
        )
        console.log(data)
        ctx.response.status = 200;
        ctx.body = {
            code: 200,
            msg: '查询成功',
            data

        }
    }



}

module.exports = SecCategory_Controller;